package org.jeecg.modules.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import liquibase.pro.packaged.L;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.entity.ZnzfEmployeeRoomInfoParam;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.demo.base.service.IZnzfEmployeeService;
import org.jeecg.modules.demo.projectroom.entity.ZnzfBed;
import org.jeecg.modules.demo.projectroom.entity.ZnzfProject;
import org.jeecg.modules.demo.projectroom.entity.ZnzfRoom;
import org.jeecg.modules.demo.projectroom.mapper.ZnzfBedMapper;
import org.jeecg.modules.demo.projectroom.mapper.ZnzfProjectMapper;
import org.jeecg.modules.demo.projectroom.mapper.ZnzfRoomMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Description: 职工表
 * @Author: jeecg-boot
 * @Date:   2022-03-27
 * @Version: V1.0
 */
@Service
public class ZnzfEmployeeServiceImpl extends ServiceImpl<ZnzfEmployeeMapper, ZnzfEmployee> implements IZnzfEmployeeService {

    @Resource
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    @Resource
    private ZnzfRoomMapper znzfRoomMapper;

    @Resource
    private ZnzfProjectMapper znzfProjectMapper;

    @Resource
    private ZnzfBedMapper znzfBedMapper;


    @Override
    public ZnzfEmployee searchByUid(String uid) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, uid);
        return znzfEmployeeMapper.selectOne(queryWrapper);
    }

    @Override
    public ZnzfEmployeeRoomInfoParam queryMyRoomInfoByUserId(String userId) {
        ZnzfEmployeeRoomInfoParam znzfEmployeeRoomInfoParam = new ZnzfEmployeeRoomInfoParam();
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, userId);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        if (oConvertUtils.isEmpty(znzfEmployee))
            return null;
        if(oConvertUtils.isEmpty(znzfEmployee.getRoomId()))
            return null;
        ZnzfRoom znzfRoom = znzfRoomMapper.selectById(znzfEmployee.getRoomId());
        if(oConvertUtils.isEmpty(znzfRoom))
            return null;
        ZnzfProject znzfProject = new ZnzfProject();
        if (oConvertUtils.isNotEmpty(znzfRoom)) {
            //设置房源表相关信息
            znzfEmployeeRoomInfoParam.setHouseNo(znzfRoom.getHouseNo());
            znzfEmployeeRoomInfoParam.setUnit(znzfRoom.getUnit());
            znzfEmployeeRoomInfoParam.setRoomNo(znzfRoom.getRoomNo());
            znzfProject = znzfProjectMapper.selectById(znzfRoom.getProjectId());
        }
        if (oConvertUtils.isNotEmpty(znzfProject)) {
            //设置项目表相关信息
            znzfEmployeeRoomInfoParam.setProvince(znzfProject.getProvince());
            znzfEmployeeRoomInfoParam.setCity(znzfProject.getCity());
            znzfEmployeeRoomInfoParam.setLocation(znzfProject.getLocation());
            znzfEmployeeRoomInfoParam.setProjectId(znzfProject.getId());
            znzfEmployeeRoomInfoParam.setProjectName(znzfProject.getName());
        }
        LambdaQueryWrapper<ZnzfBed> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ZnzfBed::getRoomId, znzfEmployee.getRoomId());
        ZnzfBed znzfBed = znzfBedMapper.selectOne(queryWrapper1);
        if (oConvertUtils.isNotEmpty(znzfBed)) {
            //设置床位表相关信息
            znzfEmployeeRoomInfoParam.setNo(znzfBed.getNo());
            znzfEmployeeRoomInfoParam.setArea(znzfBed.getArea());
        }
        return znzfEmployeeRoomInfoParam;
    }

    @Override
    public int withdraw(ZnzfEmployee znzfEmployee) {
        UpdateWrapper<ZnzfEmployee> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.set("room_id", null);
        userUpdateWrapper.lambda().eq(ZnzfEmployee::getId, znzfEmployee.getId());
        return znzfEmployeeMapper.update(znzfEmployee,userUpdateWrapper);
    }

    /**
     * 根据uid查找eid
     * @param uid
     * @return
     */
    @Override
    public String findEidByUid(String uid) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, uid);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        if (znzfEmployee != null) {
            return znzfEmployee.getId();
        }
        return null;
    }

    /**
     * 根据eid查找uid
     * @param eid
     * @return
     */
    @Override
    public String findUidByEid(String eid) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getId, eid);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        if (znzfEmployee != null) {
            return znzfEmployee.getUserId();
        }
        return null;
    }

    /**
     * 根据eid找到一条记录
     * @param eid
     * @return
     */
    @Override
    public ZnzfEmployee findByEid(String eid) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getId, eid);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        return znzfEmployee;
    }
}
